<template>
  <van-button
    v-if="is_followed"
    @click="onFollow"
    :loading="loading"
    class="button"
    round
    size="small"
    >已关注</van-button
  >
  <van-button
    v-else
    @click="onFollow"
    :loading="loading"
    class="button"
    color="#3296fa"
    round
    type="info"
    size="small"
    icon="plus"
    >关注</van-button
  >
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
import { mapMutations, mapState } from 'vuex'
export default {
  name: 'FollowUser',
  props: {
    is_followed: {
      type: Boolean,
      required: true
    },
    userId: {
      type: [String, Number, Object],
      required: true
    }
  },
  computed: {
    ...mapState(['articles'])
  },
  data() {
    return {
      loading: false,
      new_is_followed: false
    }
  },
  methods: {
    ...mapMutations(['SET_FOLLOW']),
    async onFollow() {
      this.loading = true
      console.log(this.articles)
      try {
        if (this.articles.is_followed) {
          //已关注,取消关注
          await deleteFollow(this.userId)
          this.SET_FOLLOW(!this.articles.is_followed)
        } else {
          await addFollow(this.userId)
          this.SET_FOLLOW(!this.articles.is_followed)
        }
        //更新视图状态

        // this.$emit('update-is_followed', !this.is_followed)
      } catch (error) {
        console.log(error)
        // let message = '操作失败,请重试'
        // if (error.response && error.response.status === 400) {
        //   message = '你不能关注你自己'
        // }
        // this.$toast(message)
      }
      this.loading = false
    }
  }
}
</script>

<style></style>
